import QtQuick 2.0
import QtQuick.Layouts 1.1
import Qt.database.sqlite 1.0
import "../mainmenu"

Item {

    property var machineList: new Array
    property var stateList: new Array
    property int itemCount: 0
    property int startNo: 1
    property int endNo: 0

    SqliteDB{
        id: db
    }

    MainmenuItem{ id: label
        imagepath: qsTr("../../images/correct.png")
        content: qsTr("校准")
        height: 30
        anchors{
            top: parent.top; left: parent.left; right: parent.right
        }
    }

    Rectangle{ id: menu
        height: 50
        color: "gray"
        anchors{
            bottom: parent.bottom; left: parent.left; right: parent.right
        }
    }

    //中间部件
    Rectangle{
        //拉风的分割线
        Canvas{ id: line
            height: 6
            contextType: "2d"
            onPaint: {
                context.lineWidth = 2;
                context.strokeStyle = "black"
                context.moveTo(0, 2)
                context.lineTo(parent.width, 2)
                context.stroke()
            }
            anchors{
                top: parent.top; left: parent.left; right: parent.right
            }
        }
        //文字
        Item{ id: note
            height: 100
            Text{
                height: 30; width: parent.width
                font.bold: true; font.pixelSize: 25
                text: qsTr("为使用系统，所有机械单元必须校准")
                verticalAlignment: Text.AlignTop; horizontalAlignment: Text.AlignLeft
                anchors.top: parent.top
            }

            Text{
                height: 30; width: parent.width
                font.pixelSize: 20
                text: qsTr("选择需要校准的机械单元。")
                verticalAlignment: Text.AlignBottom; horizontalAlignment: Text.AlignLeft
                anchors.bottom: parent.bottom
            }

            anchors{
                top: line.bottom; left: line.left; right: line.right;
            }
        }
        //列表
        ListView{ id: machinelist
            model: machinemodel; header: machineheader; delegate: machinedelegate; footer: machinefooter
            function addOne(i)
            {
                model.append({"machine": machineList[i], "state": stateList[i]})
            }

            highlight: Rectangle{
                color: "green"
            }

            anchors{
                top: note.bottom; left: note.left; right: note.right; bottom: parent.bottom
            }
        }

        anchors{
            top: label.bottom; left: label.left; right: label.right; bottom: menu.top
        }
    }

    //==========ListModel & ListDelegate &  ListHeader
    ListModel{
        id: machinemodel
    }

    Component{ id: machinedelegate
        Item{
            id: wrapper
            width: parent.width; height: 30
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    wrapper.ListView.view.currentIndex = index
                    mouse.accepted = true
                }
            }
            RowLayout{
                anchors.left: parent.left; anchors.verticalCenter: parent.verticalCenter; spacing: 8
                Text {
                    text: machine
                    color: wrapper.ListView.isCurrentItem ? "white" : "black"
                    font.pixelSize: 22
                    Layout.preferredWidth: machinelist.width/3
                }
                Text{
                    text: state
                    color: wrapper.ListView.isCurrentItem ? "white" : "black"
                    font.pixelSize: 22
                    Layout.preferredWidth: machinelist.width*2/3
                }
            }
        }
    }

    Component{ id: machineheader
        Rectangle {
            width: parent.width; height: 30
            color: "lightgray"
            RowLayout{
                anchors.left: parent.left; anchors.verticalCenter: parent.verticalCenter; spacing: 8
                Text {
                    text: qsTr("机械单元")
                    font.bold: true
                    font.pixelSize: 22
                    Layout.preferredWidth: machinelist.width/3

                }

                Text{
                    text: qsTr("状态")
                    font.bold: true
                    font.pixelSize: 22
                    Layout.preferredWidth: machinelist.width*2/3

                }
            }
        }
    }

    Component{ id: machinefooter
        Text {
            width: parent.width; height: 30;
            font.italic: true;
            color: "purple"
            verticalAlignment: Text.AlignVCenter
            text: qsTr(startNo.toString()+" 到 "+endNo.toString()+ " 共 "+ itemCount.toString())
        }
    }

    Component.onCompleted: {
        db.connectDB();
        db.getDBData(2, "select Machine, State from CorrectMachine");
        db.closeDB();
        itemCount = db.strCount()/2;
        for(var i=0 ; i< db.strCount(); i+=2)
        {
            machineList.push(db.getData(i))
            stateList.push(db.getData(i+1))
            machinelist.addOne(i/2)
            endNo ++;
        }
    }
}
